<?php
/*
 * @Author: fk 1031041088@qq.com
 * @Date: 2024-12-11 22:20:11
 * @LastEditors: fk 1031041088@qq.com
 * @LastEditTime: 2024-12-11 22:20:11
 */

require_once '../config/database.php';

header('Content-Type: application/json');

try {
    // 页面标识（这里使用固定值，因为只有一个页面）
    $pageId = 'preview_page';
    
    // 更新浏览次数
    $pdo->beginTransaction();
    
    // 检查记录是否存在
    $stmt = $pdo->prepare("SELECT view_count FROM page_views WHERE page_id = ?");
    $stmt->execute([$pageId]);
    $result = $stmt->fetch();
    
    if ($result) {
        // 更新现有记录
        $stmt = $pdo->prepare("UPDATE page_views SET view_count = view_count + 1 WHERE page_id = ?");
        $stmt->execute([$pageId]);
        $viewCount = $result['view_count'] + 1;
    } else {
        // 创建新记录
        $stmt = $pdo->prepare("INSERT INTO page_views (page_id, view_count) VALUES (?, 1)");
        $stmt->execute([$pageId]);
        $viewCount = 1;
    }
    
    $pdo->commit();
    
    echo json_encode([
        'success' => true,
        'view_count' => $viewCount
    ]);
    
} catch (Exception $e) {
    if ($pdo->inTransaction()) {
        $pdo->rollBack();
    }
    echo json_encode([
        'success' => false,
        'error' => $e->getMessage()
    ]);
}
?> 